package cn.edu.ccnu.nlp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;

public class CorpusIndex {
	
	public static Configuration configuration;
	public static HTablePool pool;
	public static HTable news;
	public static HTable blog;
	public static HTable bbs;
	public static HTable news_stat;
	public static HTable blog_stat;
	public static HTable bbs_stat;
	public static HTable news_index;
	static {
		configuration = HBaseConfiguration.create();
		
		try{
			pool = new HTablePool(configuration, 1000);
			news = (HTable)pool.getTable("news");
			//blog = (HTable)pool.getTable("blog");
			//bbs = (HTable)pool.getTable("bbs");
			news_stat = (HTable)pool.getTable("news_stat");
			//blog_stat = (HTable)pool.getTable("blog_stat");
			//bbs_stat = (HTable)pool.getTable("bbs_stat");
			news_index = (HTable)pool.getTable("news_index");
		}catch(Exception e){
			e.getStackTrace();
		}
	}
	
	/**
	 * 从模拟索引的新闻表中获取新闻信息，以date为rowkey会更加迅速
	 * @param tableName
	 * @param current_year
	 * @param current_month
	 * @param current_day
	 * @param pageSize
	 */
	public static void getNewsInfo(String tableName,int current_year,int current_month,int current_day,int pageSize){
		
		String month,day;
		ResultScanner rs = null;
		if(current_month < 10)
			month = "0"+current_month;
		else
			month = ""+current_month;
		
		if(current_day < 10)
			day = "0"+current_day;
		else
			day = ""+current_day;
		try{
				HTablePool pool = new HTablePool(configuration, 1000);
				HTable table = (HTable) pool.getTable(tableName);
				
				//List<Filter> filters = new ArrayList<Filter>();
				
				//Filter filter1 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(current_year+"-"+month+"-"+day)); 
				Filter filter2 = new PageFilter(2);
				//filters.add(filter1);
				//filters.add(filter2);
				//FilterList listFilter = new FilterList(filters);
				
				Scan scan = new Scan();
				scan.setFilter(filter2);
				rs = table.getScanner(scan);
				String url = null,value = null;    //url为新闻的url,value为新闻的time和标题，以‘，’隔开
				for(Result r: rs){
						System.out.println(new String("新闻内容："));
						for(KeyValue keyvalue:r.raw()){
							url = new String(keyvalue.getQualifier());
							value = new String(keyvalue.getValue());
							System.out.println("url：====值:" + url);
							System.out.println("title：====值:" + value.split(",")[1]);
						}
				}
		}catch(Exception e){
			e.getStackTrace();
		}finally{
			rs.close();
		
		}
	}
	
	
	/**
	 * 从*_stat里获取某天的记录数
	 */
	public static int currentDayCount(String tableName,int current_year, int current_month, int current_day){
		tableName = tableName+"_stat";
		String month,day;
		int count = 0;
		if(current_month < 10)
			month = "0"+current_month;
		else
			month = ""+current_month;
		
		if(current_day < 10)
			day = "0"+current_day;
		else
			day = ""+current_day;
		try {
			
			HTablePool pool = new HTablePool(configuration, 1000);
			HTable table = (HTable) pool.getTable(tableName);
			Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(current_year+"-"+month+"-"+day)); // 某年某月某日的数据量统计,注意日期的格式是否正确
			Scan s = new Scan();
			s.setFilter(filter);
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs) {
				for (KeyValue keyValue : r.raw()) {
						count = Integer.parseInt(new String(keyValue.getValue()));
				}
			}
			System.out.println(count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	/**
	 * 从news_index里获取某一天的所有的记录数
	 * @param tableName
	 * @param current_year
	 * @param current_month
	 * @param current_day
	 * @return
	 */
	/*public static int currentDayCount(String tableName,int current_year, int current_month, int current_day){
		tableName = tableName+"_index";
		String month,day;
		ResultScanner rs = null;
		HTablePool pool = null;
		int count = 0;
		if(current_month < 10)
			month = "0"+current_month;
		else
			month = ""+current_month;
		
		if(current_day < 10)
			day = "0"+current_day;
		else
			day = ""+current_day;
		try {
			
			pool = new HTablePool(configuration, 1000);
			HTable table = (HTable) pool.getTable(tableName);
			Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(current_year+"-"+month+"-"+day)); // 某年某月某日的数据量统计,注意日期的格式是否正确
			Scan scan = new Scan();
			scan.setFilter(filter);
			rs = table.getScanner(scan);
			for (Result r : rs) {
				for(KeyValue keyvalue:r.raw()){
					count++;
					System.out.println(count);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			rs.close();
		}
		return count;
	}
	*/
	
	/**
	 * 统计从2005年到当前的年份每个年份的数据量，返回“年份”-数量的map
	 * @param table
	 * @param yearNum
	 * @return
	 */
	public static Map<String,Integer> QueryByYears(HTable table) {
		int sum = 0;
		String year = null;
		ResultScanner rs = null;
		Map<String,Integer> yearNum  = new HashMap<String,Integer>();
		
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy");                     //获取当前年份，以便于遍历2005年到当前的年份
	    String current_year=sdf.format(new java.util.Date());  
	    System.out.println(current_year);
	    
	    
		try {
	    	
	    for(int i = 2005; i <= Integer.parseInt(current_year);i++){
	    	year = i+"";
			Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(year+".*")); // 查询一年的数据，正则表达式形式
			Scan s = new Scan();
			s.setFilter(filter);
			rs = table.getScanner(s);
			for (Result r : rs) {
				for (KeyValue keyValue : r.raw()) {
					sum += Integer.parseInt(new String(keyValue.getValue()));
				}
			}
			yearNum.put(year, sum);
			System.out.println(year+":"+sum);
			sum = 0;
	    }
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			rs.close();
		}
		return yearNum;
	}
	
	/**
	 * 统计某个年份中每个月的数据量
	 * @param tableName
	 */
	public static Map<String,Integer> QueryByMonths(HTable table,String year) {

		int sum = 0;
		ResultScanner rs = null;
		Map<String,Integer> monthNum = new HashMap<String,Integer>();
		
		String[] months = {"01","02","03","04","05","06","07","08","09","10","11","12"};
			try {
				for(int i = 0; i < months.length; i++){
				Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(year+"-"+months[i]+"-.*")); // 查询某年某月的记录数
				Scan s = new Scan();
				s.setFilter(filter);
				rs = table.getScanner(s);
				for (Result r : rs) {
					for (KeyValue keyValue : r.raw()) {
							sum += Integer.parseInt(new String(keyValue.getValue()));
					}
				}
				System.out.println(year+":month:"+months[i]+":sum:"+sum);
				monthNum.put(months[i], sum);
				sum = 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			rs.close();
		}
		return monthNum;
	}
	
	/**
	 * 统计某年某月中每一天的数据量
	 * @param tableName
	 */
	public static Map<String,Integer> QueryByDays(HTable table,String year,String month) {
		int sum = 0;
		ResultScanner rs = null;
		Map<String,Integer> dayNum = new HashMap<String,Integer>();
		String[] days = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14",
				"15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
		
		try {
			for(int i = 0; i < days.length; i++){
				
				Filter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(year+"-"+month+"-"+days[i])); // 某年某月某日的数据量统计,注意日期的格式是否正确
				Scan s = new Scan();
				s.setFilter(filter);
				rs = table.getScanner(s);
				for (Result r : rs) {
					for (KeyValue keyValue : r.raw()) {
							sum = Integer.parseInt(new String(keyValue.getValue()));
						
					}
				}
				dayNum.put(days[i], sum);
				System.out.println("day:"+days[i]+":sum:"+sum);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			rs.close();
		}
		return dayNum;
	}
	
	/**
	 * jsp页面中显示年份柱状图的方法，通过查询，返回字符串，形成柱状图
	 * @param current_year
	 * @param current_month
	 * @param current_day
	 * @param current_hour
	 * @param corpus
	 * @return
	 * @throws Exception
	 */
	public String getYearString(int current_year, int current_month, int current_day, int current_hour, String corpus) throws Exception
	{
		
		int start_year = 2005;
		int end_year  = start_year + 11;
		int years_count[] = null;
		double years_score[] = null;
		int total_count = 0;

		String key_count = corpus + "_" + "years_count";
		String key_score = corpus + "_" + "years_score";


		if(years_score==null || years_count==null)
		{
		    years_count = new int[12];
			years_score = new double[12];
			
			Map<String,Integer> map = new HashMap<String,Integer>();
			
			if(corpus.equals("news"))
				map = QueryByYears(news_stat);	
			if(corpus.equals("blog"))
				map = QueryByYears(blog_stat);
			if(corpus.equals("bbs"))
				map = QueryByYears(bbs_stat);
			
			int max = 0;
			Set set = map.keySet();
			Iterator it = set.iterator();
			while(it.hasNext()){
				String key = (String)it.next();
			try{
				System.out.println(key+":"+map.get(key));
				int y = Integer.parseInt(key);
				int c = years_count[y - start_year]  = (Integer)map.get(key);	
				System.out.println("pos:"+(y-start_year)+":value:"+c);
				max = (c > max) ? c : max;
			}catch(Exception e){
			 }
		}

			for(int i = 0; i < years_count.length; i++)
					total_count += years_count[i];

		}

		if( total_count == 0 )
			return "<graph></graph>";
		
		StringBuffer put_time_xml = new StringBuffer();
		put_time_xml.append("<graph  canvasBorderColor='cccccc'  canvasbgColor='cccccc' caption='' subcaption='' xAxisName='年' yAxisMinValue='0' yAxisName='' decimalPrecision='0' formatNumberScale='0' showNames='1' showValues='0'  showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='30' alternateHGridAlpha='5' >");


		for(int i = start_year; i <= end_year; i++)
		{
			String link =  String.format("corpus.jsp?c=%s&y=%s&m=%s&d=%s", corpus,  i, 1, 1);
			String color = ( i == current_year ) ? "FF0000" : "ff5904";
			put_time_xml.append( String.format("<set color='%s' name='%s' value='%s'  link='%s'/>", color, i,  years_count[i - start_year] , link) );
		}


		put_time_xml.append("</graph>"); 
		System.out.println(put_time_xml);
		return put_time_xml.toString();

	}
	
	
	/**
	 * jsp中月份的柱状图形成
	 * @param current_year
	 * @param current_month
	 * @param current_day
	 * @param current_hour
	 * @param corpus
	 * @return
	 * @throws Exception
	 */
	public String getMonthString(int current_year, int current_month, int current_day, int current_hour, String corpus) throws Exception
	{
		int months_count[] = null;
		double months_score[] = null;
		int  total_count = 0;

		String key_count = corpus + "_months_count_" + current_year;
		String key_score = corpus + "_months_score_" + current_year;

		if( months_score == null || months_count == null )
		{
			months_count = new int[12];
			months_score = new double[12];
			
			Map<String,Integer> map = new HashMap<String,Integer>();
			
			if(corpus.equals("news"))
				map = QueryByMonths(news_stat,""+current_year);
			if(corpus.equals("blog"))
				map = QueryByMonths(blog_stat,""+current_year);
			if(corpus.equals("bbs"))
				map = QueryByMonths(bbs_stat,""+current_year);
			
			Set set = map.keySet();
			Iterator it = set.iterator();
			int max = 0;
			while(it.hasNext()){
				String key;
				try{
					key = (String)it.next();
					int m = Integer.parseInt(key );		
					int c = months_count[m - 1]  =  (Integer)map.get(key);	
					max = (c > max) ? c : max;
				}catch(Exception e){
				 }
			}

				for(int i = 0; i < months_count.length; i++)
					total_count += months_count[i];
			
		}

		if( total_count == 0 )
			return "<graph></graph>";

		StringBuffer put_time_xml = new StringBuffer();
			put_time_xml.append("<graph canvasBorderColor='cccccc'  canvasbgColor='cccccc' caption='' subcaption='' xAxisName='月' yAxisMinValue='0' yAxisName='' decimalPrecision='0' formatNumberScale='0' showNames='1' showValues='0'  showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='30' alternateHGridAlpha='5' >");

		for(int i = 1; i <= 12; i++)
		{
			String link =  String.format("corpus.jsp?c=%s&y=%s&m=%s&d=%s", corpus, current_year, i, 1);
			String color = ( i == current_month ) ? "FF0000" : "ff5904";
			put_time_xml.append( String.format("<set color = '%s' name='%s' value='%s'  link='%s'/>", color, i,  months_count[i - 1] , link) );
		}
		put_time_xml.append("</graph>"); 
		System.out.println(put_time_xml);
		return put_time_xml.toString();

	}
	
	/**
	 * jsp中形成天数的柱状图
	 * @param current_year
	 * @param current_month
	 * @param current_day
	 * @param current_hour
	 * @param corpus
	 * @return
	 * @throws Exception
	 */
	public String getDayString(int current_year, int current_month, int current_day, int current_hour, String corpus)  throws Exception
	{
		
		
		int days_count[] = null;
		double days_score[] = null;
		int total_count = 0;
		String c_month = ""+current_month;
		
		if(current_month < 10)
			c_month = "0"+current_month;
		
		String key_count = corpus + "_days_count_" + current_year + "_" + current_month;
		String key_score = corpus + "_days_score_" + current_year + "_" + current_month;
		
		if( days_score == null || days_count == null)
		{
			days_count = new int[31];
			days_score = new double[31];

			Map<String,Integer> map = new HashMap<String,Integer>();
			if(corpus.equals("news"))
				map = QueryByDays(news_stat,""+current_year,c_month);
			if(corpus.equals("blog"))
				map = QueryByDays(blog_stat,""+current_year,c_month);
			if(corpus.equals("bbs"))
				map = QueryByDays(bbs_stat,""+current_year,c_month);
			
			Set set = map.keySet();
			Iterator it = set.iterator();
			int max = 0;
			while(it.hasNext()){
				try{
					String key = (String)it.next();
					int d = Integer.parseInt(key);		
					int c =days_count[d - 1] =  (Integer)map.get(key);		
					max = (c > max) ? c : max;
					System.out.println(key+":"+c);
				}catch(Exception e){
				 }
			}

				for(int i = 0; i < days_count.length; i++)
					total_count += days_count[i];

				//admin.putInCache(key_count, days_count);
				//admin.putInCache(key_score, days_score);
		}

		if( total_count == 0 )
			return "<graph></graph>";

		StringBuffer put_time_xml = new StringBuffer();
			put_time_xml.append("<graph  canvasBorderColor='cccccc'  canvasbgColor='cccccc' caption='' subcaption='' xAxisName='日' yAxisMinValue='0'  decimalPrecision='0' formatNumberScale='0' showNames='1' showValues='0'  showAlternateHGridColor='1' AlternateHGridColor='ff5904' divLineColor='ff5904' divLineAlpha='30' alternateHGridAlpha='5' >");
		
		for(int i = 1; i <= 31; i++)
		{
			String link =  String.format("corpus.jsp?c=%s&y=%s&m=%s&d=%s", corpus, current_year, current_month, i);
			String color = ( i == current_day ) ? "FF0000" : "ff5904";
			put_time_xml.append( String.format("<set color='%s' name='%s' value='%s'  link='%s'/>",color, i,  days_count[i - 1] , link) );
		}
		put_time_xml.append("</graph>"); 

		System.out.println(put_time_xml);
		return put_time_xml.toString();
	}
	public static void main(String[] args) throws Exception{
		CorpusIndex ci = new CorpusIndex();
		//getNewsInfo("news_index",2005,1,1,10);
		currentDayCount("news",2005,1,1);
		//QueryByYear(news_stat,new HashMap());
		//QueryByMonth(news_stat,"2006",new HashMap());
		//QueryByDays(news_stat,"2005","02");
		//ci.getYearString(2005,1,1,8,"news");
		//ci.getMonthString(2005, 1, 1, 8, "news");
		//ci.getDayString(2005, 1, 1, 8, "news");
		
		
	}

}
